package servlet;

import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.CompanyModel;
import model.CompanyModelExt;

import dal.ConnectionProvider;
import dal.GenericDal;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TestServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE html>\n" + "<html>\n"
				+ "<head><title>A Test Servlet</title></head>\n"
				+ "<body bgcolor=\"#fdf5e6\">\n" + "<h1>Test</h1>\n"
				+ "<p>Simple servlet for testing.</p>\n" + "</body></html>");

		Connection conn;
		try {
			conn = ConnectionProvider.genConnection();
			System.out.print("Connect sucessfull");

			GenericDal<CompanyModel, CompanyModelExt> dal = new GenericDal<CompanyModel, CompanyModelExt>(
					new CompanyModel(), conn);

			CompanyModel query = new CompanyModel();
			query.setId(1);

			CompanyModel model = (CompanyModel) dal.select(query);
			// return model;

			System.out.print(model.getFullName());

			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// try {
		// Class.forName("org.postgresql.Driver");
		// } catch (ClassNotFoundException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// // // String url = "jdbc:postgresql://localhost:5432/test";
		// String url = "jdbc:postgresql://localhost:5432/postgres";
		// Properties props = new Properties();
		// props.setProperty("user", "postgres");
		// props.setProperty("password", "password");
		// // props.setProperty("ssl","true");
		// try {
		// Connection conn = DriverManager.getConnection(url, props);
		//
		// System.out.print("Connect sucessfull");
		// } catch (SQLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
